package enterprisePerson

import (
	"context"
	"errors"
	"fmt"
	"github.com/google/uuid"
	"gorm.io/gorm"
	"insighttracking/apps/admin/auth"
	"insighttracking/common/enum"
	"insighttracking/query/dao"
	"insighttracking/query/model"
	"insighttracking/query/service"
	"net/http"

	"insighttracking/apps/admin/internal/svc"
	"insighttracking/apps/admin/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
	"os"
)

type CreateEnterprisePersonApiLogic struct {
	logx.Logger
	ctx     context.Context
	svcCtx  *svc.ServiceContext
	Request *http.Request
}

func NewCreateEnterprisePersonApiLogic(ctx context.Context, svcCtx *svc.ServiceContext, request *http.Request) *CreateEnterprisePersonApiLogic {
	hostname, _ := os.Hostname()
	return &CreateEnterprisePersonApiLogic{
		Logger: logx.WithContext(ctx).WithCallerSkip(0).WithFields(
			logx.Field("Log_UUID", uuid.New().String()),
			logx.Field("hostname", hostname)),
		ctx:     ctx,
		svcCtx:  svcCtx,
		Request: request,
	}
}

func (l *CreateEnterprisePersonApiLogic) CreateEnterprisePersonApi(req *types.CreateEnterprisePersonReq) (resp string, err error) {
	accountInfo, err := auth.GetAccountInfo(l.svcCtx, l.Request)
	if err != nil {
		return "", err
	}
	if accountInfo.AccountType != enum.EnterpriseEnum {
		return "", errors.New("只有企业账号才可以创建员工")
	}
	_, err = dao.EnterprisePersonEntity.WithContext(l.ctx).Where(dao.EnterprisePersonEntity.Mobile.Eq(req.Mobile)).First()
	if !errors.Is(err, gorm.ErrRecordNotFound) {
		return "", errors.New("手机号码已经存在,不能重复")
	}
	// 判断当前角色id不能是门店的角色id
	roleEntity, err := service.RoleService.GetByIdApi(l.ctx, req.RoleID)
	if errors.Is(err, gorm.ErrRecordNotFound) {
		return "", errors.New("传递的角色id不存在")
	}
	if roleEntity.RoleCode == enum.RoleCode01Enum {
		return "", errors.New("不能选择门店角色")
	}
	var enterprisePersonData = model.EnterprisePersonEntity{
		EnterpriseID: accountInfo.EnterpriseId, // 关联到enterprise表主键id
		Name:         req.Name,                 // 员工姓名
		Mobile:       req.Mobile,               // 员工手机号码
		Email:        req.Email,                // 员工电邮地址
		RoleID:       req.RoleID,               // 角色id,关联到role表主键id
	}
	tx := dao.Use(l.svcCtx.MysqlDb).Begin()
	if err = tx.EnterprisePersonEntity.WithContext(l.ctx).Create(&enterprisePersonData); err != nil {
		if err = tx.Rollback(); err != nil {
			return "", err
		}
		return "", err
	}
	// 2.创建账号
	if err = service.AccountService.CreateApi(l.ctx, tx, &model.AccountEntity{
		Username:     req.Username,              // 用户名
		Password:     req.Password,              // 密码
		ParentID:     accountInfo.AccountId,     // 父节点id
		AccountType:  enum.EnterprisePersonEnum, // 账号类型,0表示超管,1表示后台运营人员,2表示企业账号,3表示门店账号，4表示企业内部员工
		EnterpriseID: accountInfo.EnterpriseId,  // 企业id,enterprise表主键id
		PersonID:     enterprisePersonData.ID,   // 员工id
	}); err != nil {
		if err1 := tx.Rollback(); err1 != nil {
			fmt.Println(err1, "??aa")
			return "", err1
		}
		fmt.Println(err, "aaa")
		return "", err
	}
	if err = tx.Commit(); err != nil {
		return "", err
	}
	return "创建成功", nil
}
